Skip to content

Method: addEntityClassAssertion(AxiomValueGatherer, Object, Descriptor)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.oom;
16:
17: import java.lang.reflect.Field;
18: import java.net.URI;
19:
20: import cz.cvut.kbss.jopa.model.annotations.OWLClass;
21: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
22: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
23: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
24: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
25: import cz.cvut.kbss.jopa.oom.exceptions.EntityDeconstructionException;
26: import cz.cvut.kbss.ontodriver.model.Assertion;
27: import cz.cvut.kbss.ontodriver.model.NamedResource;
28: import cz.cvut.kbss.ontodriver.model.Value;
29:
30: class EntityDeconstructor {
31:
32: private final EntityMappingHelper mapper;
33: private CascadeResolver cascadeResolver;
34:
35: EntityDeconstructor(ObjectOntologyMapperImpl mapper) {
36: this.mapper = mapper;
37: }
38:
39: void setCascadeResolver(CascadeResolver cascadeResolver) {
40: this.cascadeResolver = cascadeResolver;
41: }
42:
43: <T> AxiomValueGatherer mapEntityToAxioms(URI primaryKey, T entity, EntityType<T> et,
44: Descriptor descriptor) {
45: assert primaryKey != null;
46:
47: final AxiomValueGatherer valueBuilder = createAxiomValueBuilder(primaryKey, descriptor);
48: try {
49: addEntityClassAssertion(valueBuilder, entity, descriptor);
50: if (et.getTypes() != null) {
51: addAssertions(entity, et, et.getTypes(), descriptor, valueBuilder);
52: }
53: if (et.getProperties() != null) {
54: addAssertions(entity, et, et.getProperties(), descriptor, valueBuilder);
55: }
56: for (Attribute<? super T, ?> att : et.getAttributes()) {
57: addAssertions(entity, et, att, descriptor, valueBuilder);
58: }
59:
60: } catch (IllegalArgumentException | IllegalAccessException e) {
61: throw new EntityDeconstructionException(e);
62: }
63: return valueBuilder;
64: }
65:
66: private static AxiomValueGatherer createAxiomValueBuilder(URI primaryKey, Descriptor descriptor) {
67: return new AxiomValueGatherer(NamedResource.create(primaryKey), descriptor.getContext());
68: }
69:
70: private <T> void addEntityClassAssertion(AxiomValueGatherer valueBuilder, T entity, Descriptor descriptor)
71: throws IllegalAccessException {
72: final OWLClass clsType = entity.getClass().getAnnotation(OWLClass.class);
73:• assert clsType != null;
74: final Assertion entityClassAssertion = Assertion.createClassAssertion(false);
75: valueBuilder.addValue(entityClassAssertion, new Value<>(URI.create(clsType.iri())),
76: descriptor.getContext());
77: }
78:
79: private <T> void addAssertions(T entity, EntityType<T> et,
80: FieldSpecification<? super T, ?> fieldSpec, Descriptor entityDescriptor,
81: final AxiomValueGatherer valueBuilder) throws IllegalAccessException {
82: final FieldStrategy<? extends FieldSpecification<? super T, ?>, T> fs = FieldStrategy
83: .createFieldStrategy(et, fieldSpec, entityDescriptor.getAttributeDescriptor(fieldSpec), mapper);
84: fs.setCascadeResolver(cascadeResolver);
85: fs.buildAxiomValuesFromInstance(entity, valueBuilder);
86: }
87:
88: <T> AxiomValueGatherer mapFieldToAxioms(URI primaryKey, T entity, Field field, EntityType<T> et,
89: Descriptor descriptor) {
90: final FieldSpecification<? super T, ?> fieldSpec = et
91: .getFieldSpecification(field.getName());
92: final AxiomValueGatherer valueBuilder = createAxiomValueBuilder(primaryKey, descriptor);
93: try {
94: addAssertions(entity, et, fieldSpec, descriptor, valueBuilder);
95: } catch (IllegalAccessException e) {
96: throw new EntityDeconstructionException(e);
97: }
98: return valueBuilder;
99: }
100: }